<?php
namespace app\admin\model;

use think\Model;
use think\Db;
use think\facade\Config;
class Classpay extends Model
{
	/*查询数据 */
	public function get_data(){
        $req=request()->param();
        if(!empty($req['start_time'])||!empty($req['end_time'])||!empty($req['cname'])){
            if(!empty($req['start_time'])){
                $sear_where[]=['create_time','>=',$req['start_time']." 00:00:00"];
            }
            if(!empty($req['end_time'])){
                $sear_where[]=['create_time','<',$req['end_time']." 23:59:59"];
            }
            if(!empty($req['cname'])){
                $sear_where[]=['cname','like',"%".$req['cname']."%"];
            }
            $list=Classpay::where($sear_where)
                ->where([['status','=',1]])
                ->order('create_time','desc')
                ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }else{
            $list=Classpay::where([['status','=',1]])->order('create_time','desc')
                ->paginate(Config::get('app.admin_page_number'),false,['query' => $req]);
        }
        $data['list']=$list;
        if(empty($req['start_time'])){$data['start_time']='';}else{$data['start_time']=$req['start_time'];}
        if(empty($req['end_time'])){$data['end_time']='';}else{$data['end_time']=$req['end_time'];}
        if(empty($req['cname'])){$data['cname']='';}else{$data['cname']=$req['cname'];}
        return $data;
        
	}
	/*获取一条数据用于修改*/
	public function get_edit($id){
        $data=Classpay::where([['status','=',1],['id','=',$id]])->find();
        return $data;  
	}
    
	/*新增*/
	public function insert_data($cname,$url,$seq){
        $Classpay = new Classpay;
        $result=$Classpay->save([
            'cname'  =>  trim($cname),
            'url' =>  trim($url),
            'seq' =>  trim($seq),
            'status' =>  1,
            'create_time' => date("Y-m-d H:i:s",time())
        ]);
        return $result;
	}
	/*修改*/
	public function update_data($id,$cname,$url,$seq){
        $data = Classpay::get($id);
        $data->cname = $cname;
        $data->url = $url;
        $data->seq = $seq;
        $result=$data->save();
		return $result;    
	}
	/*删除假动作实际为修改操作*/
	public function delete_data($id){
        $data = Classpay::get($id);
        if($data['status']!=1){
        	$result=['status'=>'0','msg'=>'无需操作'];
	        return json($result);
        }
		$data->status = '2';
		$result=$data->save();
		return $result;
	}
}
 